package cn.ctvonline.android.kaopu.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import cn.ctvonline.android.kaopu.R;

/**
 * Created by dujian on 15/11/24.
 */
public class LazyViewPager extends ViewPager {
    private boolean isBlockTouchEvent;

    public LazyViewPager(Context context) {
        super(context);
        initAttrs(context, null, 0);
    }

    public LazyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs, 0);
    }

    private void initAttrs(Context context, AttributeSet attrs, int defStyle) {
        if (attrs != null) {
            TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LazyViewPager, defStyle, 0);
            int count = typedArray.getIndexCount();
            for (int i = 0; i < count; i++) {
                //得到xml中的索引
                int index = typedArray.getIndex(i);
                switch (index) {
                    case R.styleable.LazyViewPager_blockTouchEvent: {
                        isBlockTouchEvent = typedArray.getBoolean(index, false);
                        setIsBlockTouchEvent(isBlockTouchEvent);
                    }
                    break;
                }
            }
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (!isBlockTouchEvent) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (!isBlockTouchEvent) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return true;
        }
    }

    public boolean isBlockTouchEvent() {
        return isBlockTouchEvent;
    }

    public void setIsBlockTouchEvent(boolean isBlockTouchEvent) {
        this.isBlockTouchEvent = isBlockTouchEvent;
    }
}
